package app.fugema.shop.main.goods.view;

import android.os.Bundle;
import android.view.LayoutInflater;

import android.view.ViewGroup;

import com.base.common.app.LoginUtils;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.LogUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.MyGridLayoutManager;
import com.base.common.view.adapter.ada.GeneralRecyclerAdapter;
import com.base.common.view.adapter.connector.BaseItemTypeInterface;
import com.base.common.view.base.BaseActivity;
import com.base.common.view.base.BaseFragment;
import com.base.common.viewmodel.BaseViewObserver;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import app.fugema.shop.R;
import app.fugema.shop.databinding.ActivityMiniShopBinding;
import app.fugema.shop.main.goods.adapter.ShopBannerItemType;
import app.fugema.shop.main.goods.adapter.ShopGoodsBaseItemMultiType;
import app.fugema.shop.main.goods.adapter.ShopHeaderInfoItemMultiType;
import app.fugema.shop.main.goods.adapter.ShopInfoBaseItemMultiType;
import app.fugema.shop.main.goods.model.ShopBannerData;
import app.fugema.shop.main.goods.model.ShopCouponResult;
import app.fugema.shop.main.goods.model.ShopHeadInfoBean;
import app.fugema.shop.main.goods.model.VShopResult;
import app.fugema.shop.main.goods.vm.GoodsVM;

public class ShopMiniFragment extends BaseFragment<ActivityMiniShopBinding, GoodsVM>
{


    //微店id
    private int shopId;
    private GeneralRecyclerAdapter adapter;

    private VShopResult shopResult;

    private LinearLayoutManager mGridLayoutMgr;
    private String userKey = "";
    private ShopHeadInfoBean shopHeadInfo;

    public static  ShopMiniFragment getInstance(int mShopId )
    {
        ShopMiniFragment shopMiniFragment = new ShopMiniFragment();
        Bundle args = new Bundle();
        args.putInt("shopId", mShopId);
        shopMiniFragment.setArguments(args);
        return shopMiniFragment;
    }


    @Override
    protected ActivityMiniShopBinding initDataBinding(LayoutInflater inflater, ViewGroup container)
    {
        return DataBindingUtil.inflate(inflater, R.layout.activity_mini_shop, container, false);
    }



    @Override
    public void initView()
    {
        super.initView();
        setTitle("推荐");
        shopId = getArguments().getInt("shopId",0);

        adapter = new GeneralRecyclerAdapter() {
            @Override
            public void initMultiItemType() {
                putMultiItemType(new ShopHeaderInfoItemMultiType());
                putMultiItemType(new ShopBannerItemType());
                putMultiItemType(new ShopInfoBaseItemMultiType());
                putMultiItemType(new ShopGoodsBaseItemMultiType());
            }
        };

        mGridLayoutMgr = new MyGridLayoutManager(getActivity(), 2);
        binding.rvRecyclerView.setLayoutManager(mGridLayoutMgr);

        binding.rvRecyclerView.setAdapter(adapter);

    }


    @Override
    public void initData()
    {
        super.initData();
        if( LoginUtils.isLogin() )
        {
            userKey = LoginUtils.getToken();
        }
        getShopData();

    }


    private void getShopData()
    {
        viewModel.getShopById(shopId).observe(this, new BaseViewObserver<LiveDataWrapper<VShopResult>>(viewUtils.getStateLayout())
        {
            @Override
            protected void onSuccess(LiveDataWrapper<VShopResult> data)
            {
                shopResult = data.data;
                shopHeadInfo = new ShopHeadInfoBean();
                shopHeadInfo.setItemType(BaseItemTypeInterface.TYPE_HEAD_ONE);
                shopHeadInfo.setShopBean( shopResult.getVShop() );
                if( UIUtils.isNotEmpty( shopResult.getCoupon() ) )
                {
                    shopHeadInfo.setCoupon(shopResult.getCoupon());
                }
                adapter.addHeaderView(shopHeadInfo, 0);

                //店铺 轮播图
                ShopBannerData bannerData = new ShopBannerData();
                bannerData.setItemType(BaseItemTypeInterface.TYPE_HEAD);
                bannerData.setData( shopResult.getSlideImgs() );
                adapter.addHeaderView(bannerData, 1);
                //店铺信息   先去掉 隐藏掉
                 //shopResult.getVShop().setItemType(BaseItemTypeInterface.TYPE_HEAD_TWO);
                //adapter.addHeaderView(shopResult.getVShop(), 2);
                // 店铺 推荐商品
                adapter.setDataList( data.data.getProducts() );



            }

            @Override
            public boolean isRefreshLayoutData() {
                return true;
            }

            @Override
            public boolean isEmptyData(LiveDataWrapper<VShopResult> data)
            {
                // || (data.data.getData().getCurrent() == 1 && UIUtils.isEmpty(data.data.getData().getRecords())
                return UIUtils.isEmpty(data.data);
            }
        });



    }


    private void queryShopCoupon()
    {
        //店铺优惠券信息
        viewModel.getShopCouponListByShopId( shopResult.getVShop().getShopId() ,userKey )
                .observe(getActivity(), new BaseViewObserver<LiveDataWrapper<ShopCouponResult>>() {
                    @Override
                    protected void onSuccess(LiveDataWrapper<ShopCouponResult> data)
                    {
                        LogUtil.e( "店铺优惠券信息" );
                        //{"success":false,"msg":"该店铺没有可领优惠券","code":0}

                        if( data.data.isSuccess() )
                        {
                            if( !data.data.getCoupon().isEmpty() )
                            {
                                int couponId = data.data.getCoupon().get(0).getCouponId();
                                LogUtil.e( "开始领取优惠券" );
                                        /*
                                        viewModel.applyCoupon(shopResult.getVShop().getShopId(),couponId ,userKey  )
                                                 .observe(ShopActivity.this, new BaseViewObserver<LiveDataWrapper<BaseApiResult>>()
                                                 {
                                                     @Override
                                                     protected void onSuccess(LiveDataWrapper<BaseApiResult> data)
                                                     {
                                                         //{"success":true,"msg":"", "code":1}
                                                         if( data.data.isSuccess() )
                                                         {
                                                             ToastUtils.showShortToast(ShopActivity.this, "领取成功" );
                                                         }
                                                         else
                                                         {
                                                             ToastUtils.showShortToast(ShopActivity.this, data.data.getMsg() );
                                                         }
                                                     }
                                                 });
                                        */

                                ((BaseActivity)getActivity()).addFragment(  ShopCouponDialogFragment.getInstance(data.data.getCoupon(),0  ,null) );
                            }
                        }
                    }
                });
    }


}
